package com.gton.controller;

import com.gton.annotation.SwaggerScanClass;
import com.gton.entity.GpLogin;
import com.gton.io.Resp;
import com.gton.service.DubboRPCThirdSysRouterService;
import com.gton.service.GpLoginService;
import com.gton.util.ContextCommonMsg;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: GuoTong
 * @createTime: 2022-09-24 14:37
 * @since JDK 1.8 OR 11
 **/
@RestController
@Api(tags = "测试接口")
@SwaggerScanClass
@Slf4j
public class HelloController {

    @Value("${user.name:zhangsan}")
    private String name;

    @Value("${user.age:100}")
    private int age;


    @Autowired
    private GpLoginService gpLoginService;


    /**
     * Description: Dubbo RPC调用服务方
     *
     * @author: GuoTong
     * @date: 2022-09-24 17:50:08
     * @param null
     * @return:
     */
    @Autowired
    private DubboRPCThirdSysRouterService dubboRPCThirdSysRouterService;


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ApiOperation(value = "hello", notes = "json格式")
    @ApiResponse(code = 200, message = "返回的是json格式", response = Resp.class)
    public Resp hello() {
        Map<String, Object> data = new HashMap<>();
        data.put("call-UrI", "hello");
        data.put("port", "8089");
        data.put("service-Name", "cloud-nacos");
        data.put("server-type", "nacos");
        data.put("server-addr", "127.0.0.1:8848");
        data.put("swagger-Uri", "doc.html");
        data.put("nacos-properties-name", name);
        data.put("nacos-properties-age", age);
        return Resp.Ok(data);
    }


    @RequestMapping(value = "/dubbo_get_thirdsysrouter", method = RequestMethod.GET)
    @ApiOperation(value = "usermanager服务调用thirdsysrouter", notes = "json格式")
    @ApiResponse(code = 200, message = "返回的是json格式", response = Resp.class)
    public Resp getThirdSysRouterInDubbo(HttpServletRequest request) {
        String traceId = MDC.get(ContextCommonMsg.TRACE_ID);
        String username = request.getParameter("username");
        if (StringUtils.isEmpty(username)) {
            username = "郭童";
        }
        return dubboRPCThirdSysRouterService.getUserManagerService_Hello(traceId, username);
    }


    @GlobalTransactional
    @RequestMapping(value = "/dubbo_seata_thirdsysrouter", method = RequestMethod.GET)
    @ApiOperation(value = "usermanager服务调用thirdsysrouter分布式事务", notes = "json格式")
    @ApiResponse(code = 200, message = "返回的是json格式", response = Resp.class)
    public Resp getThirdSysRouterInDubboBySeata(HttpServletRequest request, @RequestParam("isError") boolean isError) {
        String traceId = MDC.get(ContextCommonMsg.TRACE_ID);
        String username = request.getParameter("username");
        if (StringUtils.isEmpty(username)) {
            username = "郭童";
        }
        // 插入本地数据库
        GpLogin gpLogin = new GpLogin().
                setUsername("GlobalTransactional" + username)
                .setPassword("dubbo_seata_thirdsysrouter")
                .setWelcomeName("分布式事务" + traceId);
        gpLoginService.save(gpLogin);
        log.info("第一段插入数据库study库成功" + gpLogin.getId());
        Resp resp = dubboRPCThirdSysRouterService.databaseUpdate(gpLogin);
        log.info("分布式事务----结束,第二段插入数据库study02-id={}，响应码=" + resp.getCode(), resp.getData());
        if (isError) {
            int j = 100 / 0;
        }
        return resp;
    }
}
